
package vv.lines.view.swing.animation;

import vv.lines.core.Ball;
import vv.lines.core.animations.AbstractAnimation;
import vv.lines.core.animations.AnimationType;

import java.util.Collection;

import static vv.lines.view.swing.utils.SwingLinesConstants.*;

/**
 *
 * @author vpotapenko
 */
public class SwingAlphaAnimation extends AbstractAnimation {

    private double alpha;

    private boolean increase;

    public SwingAlphaAnimation(boolean increase) {
        this.increase = increase;

        alpha = increase ? MIN_ALPHA : MAX_ALPHA;
        updateView();
    }

    public boolean hasAnimation() {
        return (increase && alpha < MAX_ALPHA) || (!increase && alpha > MIN_ALPHA);
    }

    @Override
    protected void updateAnimationState() {
        alpha = increase ? alpha + ALPHA_STEP : alpha - ALPHA_STEP;
    }

    protected void updateView() {
        Collection<Ball> balls = getTarget();
        if (balls == null) return;

        for (Ball ball : balls) {
            ball.setAlpha((float) alpha);
        }
    }

    public AnimationType getType() {
        return increase ? AnimationType.SHOW : AnimationType.HIDE;
    }
}
